// ignore_for_file: prefer_const_constructors

import 'dart:async';

import 'package:flutter/material.dart';

class QGBannerWidget extends StatefulWidget {
  var animSwitch;
  final int count;
  final IndexedWidgetBuilder itemBuilder;
  void Function(int index)? onTap;
  void Function(int index) onPageChanged;

  QGBannerWidget(
      {Key? key,
      required this.count,
      required this.itemBuilder,
      required this.onPageChanged,
      this.onTap,
      this.animSwitch = true})
      : super(key: key);

  @override
  State<QGBannerWidget> createState() => _QGBannerWidgetState();
}

class _QGBannerWidgetState extends State<QGBannerWidget> {
  PageController controller = PageController(initialPage: 99999999);
  Timer? timer;
  int index = 0;
  bool inBack = false;
  List<StreamSubscription> subs = [];

  @override
  void dispose() {
    for (final sub in subs) {
      sub.cancel();
    }

    controller.dispose();
    timer?.cancel();
    super.dispose();
  }

  @override
  initState() {
    super.initState();

    auto();
  }

  auto() {
    if (!widget.animSwitch) {
      return;
    }
    timer?.cancel();
    timer = Timer.periodic(Duration(seconds: 5), (timer) {
      if (!widget.animSwitch || !TickerMode.of(context) || inBack) {
        return;
      }

      //判断是否已滑出屏幕
      RenderObject? renderObject = context.findRenderObject();
      if (renderObject != null) {
        var size = renderObject.paintBounds.size;
        var vector3 = renderObject.getTransformTo(null).getTranslation();
        if (vector3.y < size.height * -1) {
          return;
        }
      }

      if (controller.page != null) {
        var dur = Duration(milliseconds: 300);
        controller.nextPage(duration: dur, curve: Curves.easeInOut);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: GestureDetector(
            onTap: () {
              if (widget.onTap != null) {
                widget.onTap!(index);
              }
            },
            child: Listener(
              onPointerDown: (event) {
                timer?.cancel();
              },
              onPointerUp: (event) {
                auto();
              },
              child: NotificationListener(
                onNotification: (noti) {
                  return true;
                },
                child: PageView.builder(
                  onPageChanged: (idx) {
                    index = idx % widget.count;
                    widget.onPageChanged(index);
                  },
                  scrollDirection: Axis.horizontal,
                  controller: controller,
                  itemBuilder: (buildContext, idx) {
                    index = idx % widget.count;
                    return widget.itemBuilder(buildContext, index);
                  },
                  itemCount: 99999999 + widget.count + 99999999,
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }
}
